<template>
  <div class="cart-page">
    <header class="header">
      <div class="header-content">
        <div class="logo" @click="$router.push('/home')">商城</div>
        <div class="user-actions">
          <span class="cart-icon active">🛒</span>
          <span class="user-icon">👤</span>
        </div>
      </div>
    </header>

    <main class="cart-content">
      <div class="cart-header">
        <h1>购物车</h1>
        <span class="item-count">共 {{ totalItems }} 件商品</span>
      </div>

      <template v-if="cartItems.length > 0">
        <div class="cart-list">
          <div class="cart-item" v-for="item in cartItems" :key="item.id">
            <div class="item-main">
              <img :src="item.image" :alt="item.name">
              <div class="item-info">
                <div class="item-name">{{ item.name }}</div>
                <div class="item-price">¥{{ item.price }}</div>
              </div>
            </div>
            <div class="item-actions">
              <div class="quantity-control">
                <button @click="updateQuantity(item.id, -1)">-</button>
                <span>{{ item.quantity }}</span>
                <button @click="updateQuantity(item.id, 1)">+</button>
              </div>
              <div class="item-subtotal">
                <div class="subtotal-amount">¥{{ item.price * item.quantity }}</div>
              </div>
              <button class="delete-btn" @click="removeItem(item.id)">×</button>
            </div>
          </div>
        </div>

        <div class="cart-footer">
          <div class="total">
            总计: <span class="total-price">¥{{ totalPrice }}</span>
          </div>
          <button class="checkout-btn" @click="showPayment">
            结算
          </button>
        </div>
      </template>

      <div v-else class="empty-cart">
        <div class="empty-icon">🛒</div>
        <p>购物车还是空的</p>
        <button class="continue-shopping" @click="$router.push('/home')">
          去购物
        </button>
      </div>
    </main>

    <!-- 支付二维码弹窗 -->
    <div class="payment-modal" v-if="showQRCode" @click="closePayment">
      <div class="modal-content" @click.stop>
        <h3>请扫码支付</h3>
        <img src="../assets/qrcode.png" alt="支付二维码">
        <div class="payment-amount">¥{{ totalPrice }}</div>
        <button class="close-btn" @click="closePayment">关闭</button>
      </div>
    </div>
  </div>
</template>

<script>
import jinzhanhuaImg from '../assets/products/jinzhanhua.png'
import cpbImg from '../assets/products/CPB.png'
import nb530Img from '../assets/products/nb530.png'
import kiehlsmaskImg from '../assets/products/kiehlsmask.png'
import cpblotionImg from '../assets/products/cpblotion.png'
import dovechocolateImg from '../assets/products/dovechocolate.png'
import clarinsImg from '../assets/products/clarins.png'
import givenchyImg from '../assets/products/givenchy.png'

export default {
  name: 'CartView',
  data() {
    return {
      cartItems: [],
      showQRCode: false,
      imageMap: {
        '@/assets/products/jinzhanhua.png': jinzhanhuaImg,
        '@/assets/products/CPB.png': cpbImg,
        '@/assets/products/nb530.png': nb530Img,
        '@/assets/products/kiehlsmask.png': kiehlsmaskImg,
        '@/assets/products/cpblotion.png': cpblotionImg,
        '@/assets/products/dovechocolate.png': dovechocolateImg,
        '@/assets/products/clarins.png': clarinsImg,
        '@/assets/products/givenchy.png': givenchyImg
      }
    }
  },
  computed: {
    totalItems() {
      return this.cartItems.reduce((sum, item) => sum + item.quantity, 0)
    },
    totalPrice() {
      return this.cartItems.reduce((sum, item) => sum + (item.price * item.quantity), 0)
    }
  },
  created() {
    this.loadCartItems()
  },
  methods: {
    loadCartItems() {
      const items = localStorage.getItem('cart')
      const cartItems = items ? JSON.parse(items) : []
      this.cartItems = cartItems.map(item => ({
        ...item,
        image: this.imageMap[item.image] || item.image
      }))
    },
    updateQuantity(itemId, change) {
      const item = this.cartItems.find(item => item.id === itemId)
      if (item) {
        const newQuantity = item.quantity + change
        if (newQuantity > 0) {
          item.quantity = newQuantity
          this.saveCart()
        }
      }
    },
    removeItem(itemId) {
      const index = this.cartItems.findIndex(item => item.id === itemId)
      if (index > -1) {
        this.cartItems.splice(index, 1)
        this.saveCart()
      }
    },
    saveCart() {
      localStorage.setItem('cart', JSON.stringify(this.cartItems))
    },
    showPayment() {
      if (this.cartItems.length === 0) {
        alert('购物车是空的')
        return
      }
      this.showQRCode = true
    },
    closePayment() {
      this.showQRCode = false
    }
  }
}
</script>

<style scoped>
.cart-page {
  min-height: 100vh;
  background: #f7f8fa;
}

.header {
  background: white;
  padding: 15px 0;
  box-shadow: 0 1px 4px rgba(0,0,0,0.05);
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo {
  font-size: 24px;
  font-weight: bold;
  color: #5468ff;
  cursor: pointer;
}

.user-actions {
  display: flex;
  gap: 20px;
  font-size: 24px;
}

.cart-icon.active {
  color: #5468ff;
}

.cart-content {
  max-width: 1200px;
  margin: 30px auto;
  padding: 0 20px;
}

.cart-header {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 20px;
}

.cart-header h1 {
  font-size: 24px;
  font-weight: 500;
  color: #333;
}

.item-count {
  color: #666;
}

.cart-list {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0,0,0,0.05);
}

.cart-item {
  padding: 20px;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.cart-item:last-child {
  border-bottom: none;
}

.item-main {
  display: flex;
  gap: 20px;
  flex: 1;
}

.item-main img {
  width: 100px;
  height: 100px;
  object-fit: contain;
  background: #fff;
  border-radius: 8px;
  padding: 10px;
}

.item-info {
  flex: 1;
}

.item-name {
  font-size: 16px;
  color: #333;
  margin-bottom: 10px;
}

.item-price {
  color: #5468ff;
  font-weight: 500;
}

.item-actions {
  display: flex;
  align-items: center;
  gap: 30px;
}

.quantity-control {
  display: flex;
  align-items: center;
  gap: 10px;
}

.quantity-control button {
  width: 28px;
  height: 28px;
  border: 1px solid #ddd;
  border-radius: 4px;
  background: white;
  color: #333;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.quantity-control span {
  min-width: 40px;
  text-align: center;
}

.item-subtotal {
  min-width: 80px;
  text-align: right;
}

.subtotal-amount {
  color: #5468ff;
  font-weight: 500;
}

.delete-btn {
  color: #999;
  font-size: 20px;
  cursor: pointer;
  padding: 5px;
}

.delete-btn:hover {
  color: #ff4d4f;
}

.cart-footer {
  margin-top: 20px;
  background: white;
  border-radius: 12px;
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.total {
  font-size: 16px;
  color: #333;
}

.total-price {
  font-size: 24px;
  font-weight: bold;
  color: #5468ff;
}

.checkout-btn {
  padding: 12px 40px;
  background: #5468ff;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
}

.empty-cart {
  text-align: center;
  padding: 60px 20px;
  background: white;
  border-radius: 12px;
}

.empty-icon {
  font-size: 48px;
  color: #999;
  margin-bottom: 20px;
}

.empty-cart p {
  color: #666;
  margin-bottom: 30px;
}

.continue-shopping {
  padding: 12px 30px;
  background: #5468ff;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
}

@media (max-width: 768px) {
  .cart-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }

  .item-actions {
    width: 100%;
    justify-content: space-between;
  }
}

.payment-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  padding: 30px;
  border-radius: 12px;
  text-align: center;
  max-width: 320px;
  width: 90%;
}

.modal-content h3 {
  font-size: 18px;
  color: #333;
  margin-bottom: 20px;
}

.modal-content img {
  width: 200px;
  height: 200px;
  object-fit: contain;
  margin: 0 auto 20px;
}

.payment-amount {
  font-size: 24px;
  font-weight: bold;
  color: #5468ff;
  margin-bottom: 20px;
}

.close-btn {
  padding: 10px 30px;
  background: #5468ff;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.close-btn:hover {
  background: #4a5aff;
  transform: translateY(-1px);
}
</style> 